package com.criticalritrpg;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.criticalritrpg.bd.BDService;

public class VerJogosActivity extends Activity implements OnClickListener,AdapterView.OnItemClickListener{
	EditText mText;
	Button mAdd;
	ListView mList;
	BDService mHelper;
	SQLiteDatabase mDb;
	Cursor mCursor;
	SimpleCursorAdapter mAdapter;
	int ano,mes,dia,hora,min,id_jogo;
	String local;
	private final String help_mestre = "Voce entrou no modo mestre\n escolha ou crie um novo jogo\n"+
	"Ao clicar em jogo voce podera acessar as fichas de outros jogadores\n"+
			"compartilhar via bluetooth ou marcar uma nova sessao e local";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ver_jogos);
		mText = (EditText)findViewById(R.id.name);
		mAdd = (Button)findViewById(R.id.add);
		mAdd.setOnClickListener(this);
		mList = (ListView)findViewById(R.id.list);
		mList.setOnItemClickListener(this);
		mHelper = new BDService(this);
		
		helpBox("Modo mestre",help_mestre);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//Open connections to the database
		mDb = mHelper.getWritableDatabase();
		
		String[] columns = new String[] {BDService.JOGO_ID, BDService.COL_MESTRE, BDService.COL_DATE_INICIO};
		mCursor = mDb.query(BDService.TABLE_NAME_JOGOS, columns, null, null, null, null, null);
		//Refresh the list
		String[] headers = new String[] {BDService.COL_MESTRE, BDService.COL_DATE_INICIO};
		mAdapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item,
		mCursor, headers, new int[]{android.R.id.text1, android.R.id.text2});
		mList.setAdapter(mAdapter);
		
	}
	
	@Override
	public void onPause() {
		super.onPause();
		//Close all connections
		mDb.close();
		mCursor.close();
	}
	
	public void onClick(View v) {
		// TODO Auto-generated method stub
		//Add a new value to the database
		//ContentValues cv = new ContentValues(2);
		//cv.put(BDService.COL_MESTRE, mText.getText().toString());
		//Create a formatter for SQL date format
		//SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//cv.put(BDService.COL_DATE_INICIO, dateFormat.format(new Date())); //Insert 'now' as the date
		//mDb.insert(BDService.TABLE_NAME_JOGOS, null, cv);
		//Refresh the list
		mHelper.insereMestre(mText.getText().toString());
		mCursor.requery();
		mAdapter.notifyDataSetChanged();
		//Clear the edit field
		//Log.d("BD", "dia:"+dia);
		//Log.d("BD", "mes:"+mes);
		//Log.d("BD", "hora:"+hora);
		//Log.d("BD", "min:"+min);
		//GregorianCalendar g  = new GregorianCalendar(ano, mes, dia, hora, min);
		//Date d = g.getTime();
		//if(ano !=0){
			//mHelper.insereAgendaData(d, id_jogo+1, local);
			
			//Toast.makeText(getApplicationContext(),"dia: "+dia+"hora: "+hora+"min: "+min, Toast.LENGTH_SHORT).show();
			//}
		mText.setText(null);
		
		
	}

	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		// TODO Auto-generated method stub
		//Delete the item from the database
		//mCursor.moveToPosition(position);
		//Get the id value of this row
	 //String rowId = mCursor.getString(0); //Column 0 of the cursor is the id
	//	mDb.delete(BDService.TABLE_NAME_JOGOS, "_id = ?", new String[]{rowId});
		//Refresh the list
		//mCursor.requery();
		//mAdapter.notifyDataSetChanged();
		//mostra jogadores
		Log.d("BD", "position: "+position);
		
		//Add a new value to the database
		//ContentValues cv = new ContentValues(2);
		//cv.put(BDService.COL_NAME_PARTICIPANTE, mText.getText().toString());
		//Create a formatter for SQL date format
		//SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//cv.put(BDService.COL_DATE_INICIO, dateFormat.format(new Date())); //Insert 'now' as the date
		//mDb.insert(BDService.TABLE_NAME_PARTICIPANTES, null, cv);
		//Refresh the list
		//mCursor.requery();
		//mAdapter.notifyDataSetChanged();
		//Clear the edit field
		//mText.setText(null);
		
		//String[] columns = new String[] {"_idj", BDService.COL_NAME_PARTICIPANTE,BDService.COL_DATE_INICIO};
		//mCursor = mDb.query(BDService.TABLE_NAME_PARTICIPANTES, columns, null, null, null, null, null);
		//mCursor.moveToLast();
		//String num = mCursor.getString(1);
		//Toast.makeText(this, "ultimo jogador adicionado: "+num, Toast.LENGTH_LONG).show();
		//id_jogo = position;
		Intent mInten = new Intent(getApplicationContext(), VerAgendaActivity.class);
		mInten.setFlags(position);
		startActivity(mInten);
		
	}
	/*
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		//super.onActivityResult(requestCode, resultCode, data);
		ano = data.getExtras().getInt(AgendaActivity.ANO);
		mes = data.getExtras().getInt(AgendaActivity.MES);
		dia = data.getExtras().getInt(AgendaActivity.DIA);
		hora = data.getExtras().getInt(AgendaActivity.HORA);
		min = data.getExtras().getInt(AgendaActivity.MIN);
		
		local = data.getExtras().getString(AgendaActivity.LOCAL);
		
	}
	*/
	public void helpBox(String title, String txt) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		builder.setMessage(txt);
		builder.setPositiveButton("OK", null);
		AlertDialog dialog = builder.show();

		// Must call show() prior to fetching text view
		TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
		messageView.setGravity(Gravity.CENTER);

	}
}
